var mode;
var oldmatk;
var oldma;
var row;


$(document).ready(function() {
	 
});



function create(){
	mode = "add";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

	document.getElementById('matk').value = "";
	document.getElementById('tentk').value = "";
	document.getElementById('tk_me').value = "";
	$("#tieu_khoan").attr("checked",false);		
	$("#ngoaibang").attr("checked",false);		

	document.getElementById('create_title').innerHTML = "Thêm mới tài khoản";
	document.getElementById('createbutton').value = "Thêm";
	document.getElementById('matk').focus();
}    

function createDVCS(){
	mode = "add";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

	document.getElementById('ma_dvcs').value = "";
	document.getElementById('ten_dvcs').value = "";

	document.getElementById('create_title').innerHTML = "Thêm mới ĐVCS";
	document.getElementById('createbutton').value = "Thêm";
	document.getElementById('ma_dvcs').focus();
}    



function RTFcreate(){
	document.getElementById('create').style.display = 'none';
	document.getElementById('search').style.display = 'block';
	document.getElementById('btnThem').style.display = 'block';
}    

function edit(pmatk, ptentk, ida){
	mode = "edit";
	row = ida;

	var x=document.getElementById('customers');
	var new_row = x.rows[row];
	var tds = new_row.getElementsByTagName('td');
	var ips = new_row.getElementsByTagName('input')[0];
	var tieu_khoan  = $(ips).attr( "checked" );
	var ipngoaibang = new_row.getElementsByTagName('input')[1];
	if(typeof ipngoaibang === 'undefined'){
		ngoaibang = false;
		var tk_me = (tds[3].innerHTML).trim() ;
	 }
	else{
		ngoaibang = true;
		var tk_me = "";
	}


	var pmatk = (tds[0].innerHTML).trim() ;
	var ptentk = (tds[1].innerHTML).trim() ;
	oldmatk = pmatk;

	document.getElementById('matk').value = pmatk;
	document.getElementById('tentk').value = ptentk;

	if (tieu_khoan == true)
	{
		$("#tieu_khoan").attr("checked",true);
	}
	else{
		$("#tieu_khoan").attr("checked",false);

	}

	if (ngoaibang == true)
	{
		$("#ngoaibang").attr("checked",true);
	}
	else{
		$("#ngoaibang").attr("checked",false);

	}

	document.getElementById('tk_me').value = tk_me;


	document.getElementById('create_title').innerHTML = "Sửa tài khoản(" + pmatk+ "-"+ ptentk+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';
	document.getElementById('matk').focus();
}    

function editDMDV(pma, pten, ida){
	mode = "edit";
	oldma = pma;
	row = ida;
	var x=document.getElementById('customers');
	var new_row = x.rows[row];
	var tds = new_row.getElementsByTagName('td');

	var ma = tds[0].innerHTML ;
	var ten = tds[1].innerHTML ;
	var masothue = tds[2].innerHTML ;
	var diachi =  tds[3].innerHTML ;
	var dienthoai = tds[4].innerHTML ;
	var fax = tds[5].innerHTML ;
	var daidien = tds[6].innerHTML ;

	document.getElementById('madv').value = ma;
	document.getElementById('tendv').value = ten;
	document.getElementById('masothue').value = masothue;
	document.getElementById('diachi').value = diachi;
	document.getElementById('dienthoai').value = dienthoai;
	document.getElementById('fax').value = fax;
	 document.getElementById('daidien').value = daidien;

	document.getElementById('create_title').innerHTML = "Sửa(" + pma+ "-"+ pten+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

}    


function editDMDVCS(pma, pten, ida){
	mode = "edit";
	oldma = pma;
	row = ida;
	var x=document.getElementById('customers');
	var new_row = x.rows[row];
	var tds = new_row.getElementsByTagName('td');

	var ma = (tds[0].innerHTML).trim() ;
	var ten = tds[1].innerHTML ;

	document.getElementById('ma_dvcs').value = ma;
	document.getElementById('ten_dvcs').value = ten;

	document.getElementById('create_title').innerHTML = "Sửa(" + pma+ "-"+ pten+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

}    

function editDMVT(pma, pten, pdvt, ptkdt,  ptkvt, ptkgv){
	mode = "edit";
	oldma = pma;
	document.getElementById('mavt').value = pma;
	document.getElementById('tenvt').value = pten;
	document.getElementById('dvt').value = pdvt;
		$('#tk_dt').val(ptkdt);

	$('#tk_gv').val(ptkgv);

	$('#tk_vt').val(ptkvt);

	document.getElementById('create_title').innerHTML = "Sửa Vật tư (" + pma+ "-"+ pten+")";
	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';
}    



function refreshDMDV(letter, url)
{
	url = url+ "&&dmdv_initial="+ letter;
	location.href = url;
	return;

}

function refreshDMVT(letter, url)
{
	url = url+ "&&dmvt_initial="+ letter;
	location.href = url;
	return;
}

function SaveDMDV(url){
	var ma=document.getElementById('madv').value;
	var ten=document.getElementById('tendv').value;
	var masothue=document.getElementById('masothue').value;
	var diachi=document.getElementById('diachi').value;
	var dienthoai=document.getElementById('dienthoai').value;
	var fax=document.getElementById('fax').value;
	var daidien=document.getElementById('daidien').value;

	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {madv: ma, tendv: ten, masothue: masothue, diachi: diachi, dienthoai: dienthoai, fax: fax, daidien: daidien, mode: mode, oldma: oldma},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('madv').value = "";
					 document.getElementById('tendv').value = "";
					 document.getElementById('masothue').value = "";
					 document.getElementById('diachi').value = "";
					 document.getElementById('dienthoai').value = "";
					 document.getElementById('fax').value = "";
					 document.getElementById('daidien').value = "";

					var x=document.getElementById('customers');
					var new_row = x.rows[1].cloneNode(true);
					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma;
					tds[1].innerHTML = ten;
					tds[2].innerHTML = masothue;
					tds[3].innerHTML = diachi;
					tds[4].innerHTML = dienthoai;
					tds[5].innerHTML = fax;
					tds[6].innerHTML = daidien;
					tds[7].innerHTML = "";
					//x.appendChild( new_row );
					$("#customers tr:first").after(new_row);
				  }
				  else
				   {
					var x=document.getElementById('customers');
					var new_row = x.rows[row];
					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma;
					tds[1].innerHTML = ten;
					tds[2].innerHTML = masothue;
					tds[3].innerHTML = diachi;
					tds[4].innerHTML = dienthoai;
					tds[5].innerHTML = fax;
					tds[6].innerHTML = daidien;
					tds[7].innerHTML = "";
					alert("Đã ghi xong - Xin mời chọn sửa dòng khác hoặc chọn thôi để trở về!");

				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});


}

function SaveDMTK(url){
	var ma=document.getElementById('matk').value;
	var ten=document.getElementById('tentk').value;
	var tk_me=document.getElementById('tk_me').value;
	var tieu_khoan  = $("#tieu_khoan").attr( "checked" );

	if (tieu_khoan == true)
	{
		tieu_khoan = 1;
	}
	else{
		tieu_khoan = 0;
	}

	var ngoaibang  = $("#ngoaibang").attr( "checked" );

	if (ngoaibang == true)
	{
		ngoaibang = 1;
	}
	else{
		ngoaibang = 0;
	}


	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {matk: ma, tentk: ten, tieu_khoan: tieu_khoan, tk_me: tk_me, ngoaibang:ngoaibang, mode: mode, oldma: oldmatk},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('matk').value = "";
					 document.getElementById('tentk').value = ""
						$("#tieu_khoan").attr("checked",false);		$("#ngoaibang").attr("checked",false);		
					 document.getElementById('tk_me').value = "";

					var x=document.getElementById('customers');
					var new_row = x.rows[1].cloneNode(true);
					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma.toUpperCase();;
					tds[1].innerHTML = ten;
					if (tieu_khoan == 1)
					{
						tds[2].innerHTML = "<input type='checkbox' name='tieu_khoan'  checked='checked' disabled='disabled' />";  
					}
					else{
						tds[2].innerHTML = "<input type='checkbox' name='tieu_khoan' disabled='disabled' />";  
					}

					if (ngoaibang == 1)
					{
						tds[3].innerHTML = "<input type='checkbox' name='ngoaibang'  checked='checked' disabled='disabled' />TK ngoài bảng";  
					}
					else{
						tds[3].innerHTML = tk_me;

					}
					tds[4].innerHTML = "";
					$("#customers tr:first").after(new_row);
					document.getElementById('matk').focus();

				  }
				  else
				   {
					var x=document.getElementById('customers');
					var new_row = x.rows[row];
					var tds = new_row.getElementsByTagName('td');


					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma;
					tds[1].innerHTML = ten;
					if (tieu_khoan == 1)
					{
						tds[2].innerHTML = "<input type='checkbox' name='tieu_khoan'  checked='checked' disabled='disabled' />";  
					}
					else{
						tds[2].innerHTML = "<input type='checkbox' name='tieu_khoan' disabled='disabled' />";  
					}

					if (ngoaibang == 1)
					{
						tds[3].innerHTML = "<input type='checkbox' name='ngoaibang'  checked='checked' disabled='disabled' />TK ngoài bảng";  
					}
					else{
						tds[3].innerHTML = tk_me;

					}


					tds[4].innerHTML = "";
					alert("Đã ghi xong - Xin mời chọn sửa dòng khác hoặc chọn thôi để trở về!");

				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});


}

function SaveDMVT(url){
	var ma=document.getElementById('mavt').value;
	var ten=document.getElementById('tenvt').value;
	var dvt=document.getElementById('dvt').value;
	var tk_vt= $('select[name=tk_vt]').val();
	var tk_gv= $('select[name=tk_gv]').val();
	var tk_dt= $('select[name=tk_dt]').val();

	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {mavt: ma, tenvt: ten, dvt: dvt, tk_vt: tk_vt, tk_gv: tk_gv, tk_dt: tk_dt,  mode: mode, oldma: oldma},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('mavt').value = "";
					 document.getElementById('tenvt').value = "";

				  }
				  else
				   {
					  	location.reload();
				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});
}
function SaveDMDVCS(url){
	
	var ma=document.getElementById('ma_dvcs').value;
	var ten=document.getElementById('ten_dvcs').value;

	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {ma_dvcs: ma, ten_dvcs: ten, mode: mode, oldma: oldma},
       cache: false,
       success: function(response)
       {
		   if ((response.substring(0,1) == 1))
		   {
				alert(response.substring(2));
		   }
		   else{
			   p = response.indexOf('Duplicate'); 

			   if (p>=0){
				   alert("Mã này đã có rồi!");
			   }
			   else{
				  if (mode == 'add')
				  {
					 document.getElementById('ma_dvcs').value = "";
					 document.getElementById('ten_dvcs').value = ""

					var x=document.getElementById('customers');
					var new_row = x.rows[1].cloneNode(true);
					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma.toUpperCase();
					tds[1].innerHTML = ten;
					tds[2].innerHTML = "";
					$("#customers tr:first").after(new_row);
					document.getElementById('ma_dvcs').focus();

				  }
				  else
				   {
					var x=document.getElementById('customers');
					var new_row = x.rows[row];
					var tds = new_row.getElementsByTagName('td');


					var tds = new_row.getElementsByTagName('td');
					tds[0].innerHTML = ma;
					tds[1].innerHTML = ten;
					tds[2].innerHTML = "";
					alert("Đã ghi xong - Xin mời chọn sửa dòng khác hoặc chọn thôi để trở về!");

				   }
			   }
		   }
       },
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});
}



function SaveNew(host, username, password, databaseName, urlupdate ){
	alert(urlupdate);
	var matk=document.getElementById('matk').value;
	var tentk=document.getElementById('tentk').value;
	//var loaitk=document.getElementById('loaitk').value;

	if (matk == "" || tentk ==  "")
	{
		alert("Mã, Tên tài khoản không được rỗng!");
		return;
	}

	jQuery.ajax({
       type: "POST",
       url: urlupdate,
	   data: {host: host, username: username, password: password, databaseName: databaseName, matk: matk, tentk: tentk,  oldmatk: oldmatk, mode: mode},
       cache: false,
       success: function(response)
       {
		 if (response == "2")
		 {
			 alert("Mã TK này đã có rồi!");
		 }
		 else if (response == "1")
		 {
			 alert("Không có bảng DMTK!");

		 }
		 else if (response == "3")
		 {
			 alert("Co loi du lieu, mau tin khong ghi duoc!");

		 }
		 else 
		   {
			 if (mode == 'add'){
			 document.getElementById('matk').value = "";
			 document.getElementById('tentk').value = "";
			 }
			 else{
				 	location.reload();
			 }

		   }
       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});
	if (mode == 'edit')
	{
		RTFcreate();
	}


}
